package com.yhg.context.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhg.common.utils.R;
import com.yhg.context.entity.BlBlog;
import com.yhg.context.entity.vo.BlogListVo;
import com.yhg.context.feign.AdminFeign;
import com.yhg.context.service.BlBlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 博客表 前端控制器
 * </p>
 *
 * @author 凌月青缃
 * @since 2021-01-21
 */
@Api(description = "博客管理")
@RestController
@RequestMapping("/context/blog")
@CrossOrigin
public class BlBlogController {

    @Autowired
    BlBlogService blogService;

    @Autowired
    AdminFeign adminFeign;

    @GetMapping("blogToMember")
    public R blogToMember(){

        int i =10/0;
        R list = adminFeign.list();
        return R.ok().data("data",list);
    }

    /**
     * 更新博客内容
     */
    @PostMapping("update")
    public R update(@RequestBody(required = false) BlBlog blog){
        boolean b = blogService.updateById(blog);
        if (b){
            return R.ok().message("修改成功");
        }else {
            return R.error().message("修改失败");
        }

    }

    /**
     * 根据id查询博客内容
     */
    @GetMapping("selectOne/{id}")
    public R selectOne(@PathVariable("id") String id){
        return R.ok().data("data",blogService.getById(id));
    }
    /**
     * 发布博客
     * @author 凌月青缃
     */

    @PostMapping("saveBlog")
    public R saveBlog(@RequestBody BlBlog blog){
        boolean b = blogService.save(blog);
        if (b) return R.ok();
        else return R.error();
    }

    /**
     * 删除博客
     * @author 凌月青缃
     */
    @ApiOperation(value = "删除博客")
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable("id") String id){
        blogService.removeById(id);
        return R.ok();
    }

    /**
     * 查询所有博客
     * @author 凌月青缃
     */
    @GetMapping("list")
    public R list(){
        List<BlBlog> list = blogService.list();
        return R.ok().data("data",list);
    }

    /**
     * 带条件与分页功能的查询
     */
    @PostMapping("listPage/{current}/{size}")
    public R listPage(@PathVariable("current") Long current,@PathVariable("size") Long size,
                      @RequestBody(required = false) BlogListVo vo){
        //new 一个分页对象
        Page<BlBlog> page = new Page<>(current,size);
        //new 一个条件构造器
        QueryWrapper<BlBlog> wrapper = new QueryWrapper<>();

        if (vo==null){
            blogService.page(page);
        }else {
            //构造条件
            if (!StringUtils.isEmpty(vo.getBlogTitle())){
                wrapper.like("blog_title",vo.getBlogTitle());
            }
            if (!StringUtils.isEmpty(vo.getBlogType())){
                wrapper.eq("blog_type",vo.getBlogType());
            }
            if (!StringUtils.isEmpty(vo.getBegin())){
                wrapper.gt("gmt_create",vo.getBegin());
            }
            if (!StringUtils.isEmpty(vo.getEnd())){
                wrapper.lt("gmt_create",vo.getEnd());
            }
            blogService.page(page,wrapper);
        }

        List<BlBlog> records = page.getRecords();
        long total = page.getTotal();

        return R.ok().data("records",records).data("total",total);
    }


}

